package com.JUC.cha05.setc01;

import java.util.concurrent.TimeUnit;

public class InterruptDemo3 {

    public static void main(String[] args) {

        Thread t1 = new Thread(() -> {
            while (true){
                if (Thread.currentThread().isInterrupted()){
                    System.out.println(Thread.currentThread().getName() + "线程标识 = true\t退出");
                    break;
                }
                // 休眠200毫秒
                try {
                    TimeUnit.MILLISECONDS.sleep(200);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();// 没有它，线程不会停止，中断不打断
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "\t正在执行...");
            }
        }, "t1");
        t1.start();
        
        // 暂停几秒钟线程
        try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
        
        new Thread(() -> {
            t1.interrupt();
        }, "t2").start();

    }
}
